package com.usian.behavior.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.usian.behavior.service.ApFollowBehaviorService;
import com.usian.model.user.dtos.FollowBehaviorDto;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class FollowBehaviorListener {

    @Autowired
    private ApFollowBehaviorService apFollowBehaviorService;

    @KafkaListener(topics = {"followBehavior"})
    public void followBehavior(ConsumerRecord<?,?> record){
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
        if(optional.isPresent()){
            String followBehaviorDtoStr = (String) record.value();
            FollowBehaviorDto followBehaviorDto =JSON.parseObject(followBehaviorDtoStr,FollowBehaviorDto.class);
            System.out.println("关注行为获取消息====="+followBehaviorDto.toString());
            //保存用户关注行为记录表
            apFollowBehaviorService.saveFollowBehavior(followBehaviorDto);
        }
    }
}